﻿using LD.Host;
using LD.Tool;
using log4net.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YTE.Control;

namespace LD.BC
{
    public partial class FrmIoSetEdit : FrmOKCancel
    {
        public Action<TB_IO> AddAction;
        private bool _isEdit = false;
        public FrmIoSetEdit()
        {
            InitializeComponent();
            this.btn_ok.Click += Btn_ok_Click;
            Init();
        }
        public FrmIoSetEdit(TB_IO io) : this()
        {
            _isEdit = true;

            this.txt_ioNo.Text = io.No.ToString();
            this.txt_ioNo.Enabled = false;
            this.txt_ioName.Text = io.Name;
            this.cob_ioType.SelectedIndex = io.IoType;
            this.cob_ioLevel.SelectedIndex = (int)io.ValidateLevel;
            this.cob_ioFix.Text = io.FixType;
        }
        private void Btn_ok_Click(object sender, EventArgs e)
        {
            if (!int.TryParse(this.txt_ioNo.Text, out var no))
                return;
            if (!_isEdit)
            {
                //是否重复
                var isExist = BcFactory.Instance.Config.IoList.Where(p => p.IoType == this.cob_ioType.SelectedIndex && p.No == no).Any();
                if (isExist)
                {
                    Log4.Warr("IO重复");
                    return;
                }
            }

            AddAction?.Invoke(new TB_IO()
            {
                No = no,
                Name = this.txt_ioName.Text,
                IoType = this.cob_ioType.SelectedIndex,
                ValidateLevel = (LevelType)Enum.Parse(typeof(LevelType), this.cob_ioLevel.Text),
                FixType = this.cob_ioFix.Text
            });
            if (_isEdit)
                this.Close();
        }
        private DataTable GetEnum<T>()
        {
            string[] str = Enum.GetNames(typeof(T));
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private void FrmIoSetEdit_Load(object sender, EventArgs e)
        {

        }
        private void cob_ioType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cob_ioFix.DataSource = null;
            if (this.cob_ioType.Text == IoType.输入.ToString())
            {
                DataTable iofix = GetEnum<RunEnum>();
                this.cob_ioFix.DataSource = iofix;
                this.cob_ioFix.DisplayMember = "name";
                this.cob_ioFix.ValueMember = "id";
            }
            else
            {
                DataTable iofix = GetEnum<LightEnum>();
                this.cob_ioFix.DataSource = iofix;
                this.cob_ioFix.DisplayMember = "name";
                this.cob_ioFix.ValueMember = "id";
            }
        }
        private void Init()
        {
            DataTable level = GetEnum<LevelType>();
            this.cob_ioLevel.DataSource = level;
            this.cob_ioLevel.DisplayMember = "name";
            this.cob_ioLevel.ValueMember = "id";

            DataTable iotype = GetEnum<IoType>();
            this.cob_ioType.DataSource = iotype;
            this.cob_ioType.DisplayMember = "name";
            this.cob_ioType.ValueMember = "id";
        }
    }
}
